package com.attendance.www.hr.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.AttachmentBean;
import com.attendance.www.hr.beans.EmployeeBean;
import com.attendance.www.hr.beans.EmployeePageBean;
import com.attendance.www.hr.beans.UserEmployeeBean;
import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IEmployeeImportService;
//import com.attendance.www.hr.service.IEmployeeImportService;
import com.attendance.www.hr.service.IEmployeeService;
import com.zhangkongli.frame.core.ResultMap;

/**
 * 人员设置
 * 
 * @version 2017/10/12
 * @author 崔棋 张鑫
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {

	@Resource
	private IEmployeeService employeeService;
	@Resource
	private IEmployeeImportService employeeImportService;

	/**
	 * 模板下载
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getEmployeeURL.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getHolidayURL(HttpServletRequest request) {
		return AppConstant.TEMPLATE_RESOURCE + "/员工信息导入模板.xlsx";
	}

	/**
	 * 编码信息导入接口
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/importEmployee.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object importCode(HttpServletRequest request, @RequestBody AttachmentBean attachmentBean) throws Exception {

		if (attachmentBean.getType() == null || attachmentBean.getAttachmentId() == null
				|| attachmentBean.getFromDate() == null) {
			throw new ResultFaultException("0_00001");
		}

		// 第二步导入编码信息数据
		List<String> successList = employeeImportService.importCode(attachmentBean);

		return ResultMap.success(successList);
	}

	/**
	 * 获取所有员工列表
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getEmployeeList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getEmployeeList(HttpServletRequest request, @RequestBody EmployeeBean employeeBean) {
		Integer perPage = employeeBean.getPerPage();
		Integer rowCount = employeeBean.getRowCount();
		if (perPage == null || rowCount == null) {
			throw new ResultFaultException("0_00001");
		}

		Integer startNum = 0;
		if (perPage > 1) {
			startNum = (perPage - 1) * rowCount;
		}
		EmployeePageBean employeePageBean = new EmployeePageBean();
		employeePageBean = employeeService.getEmployeePageList(employeeBean, startNum, rowCount);
		return ResultMap.success(employeePageBean);
	}

	/**
	 * 获取所有员工Id列表
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getEmployeeIdList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getEmployeeIdList(HttpServletRequest request) {
		String employeeNo = request.getParameter("employeeNo");
		String roleName = request.getParameter("roleName");
		String departmentName = request.getParameter("departmentName");
		String corporationName = request.getParameter("corporationName");
		String employeeIsSeparatedString = request.getParameter("employeeIsSeparated");
		Integer employeeIsSeparated = Integer.parseInt(employeeIsSeparatedString);
		return ResultMap.success(employeeService.getEmployeeIdList(employeeNo, roleName, departmentName,
				corporationName, employeeIsSeparated));
	}

	/**
	 * 新增免签人员
	 * 
	 * @param employeeIdList
	 * @return
	 */
	@RequestMapping(value = "/updateVisaWaiver.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object updateVisaWaiver(HttpServletRequest request, @RequestBody EmployeeBean employeeBean) {
		if (employeeBean.getIsType() == null || employeeBean.getEmployeeIdList() == null
				|| employeeBean.getEmployeeIdList().size() <= 0) {
			throw new ResultFaultException("0_00001");
		}
		return ResultMap.success(employeeService.updateVisaWaiver(employeeBean));
	}

	/**
	 * 查询员工级别
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getLevelList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getLevelList() {
		return ResultMap.success(employeeService.getLevelList());
	}

	/**
	 * 新增员工
	 * 
	 * @param request
	 * @param employeeBean
	 * @return
	 */
	@RequestMapping(value = "/saveEmployee.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object saveEmployee(HttpServletRequest request, @RequestBody EmployeeBean employeeBean) {
		if (StringUtils.isBlank(employeeBean.getChineseName()) || StringUtils.isBlank(employeeBean.getEmployeeNo())
				|| employeeBean.getSex() == null || employeeBean.getEntryDate() == null
				|| employeeBean.getCorporationId() == null || employeeBean.getDepartmentId() == null
				|| employeeBean.getRoleList() == null || employeeBean.getRoleList().size() <= 0
				|| employeeBean.getFromDate() == null) {
			throw new ResultFaultException("0_00001");
		}
		employeeService.getVerifyEmployee(employeeBean);
		return ResultMap.success(employeeService.saveEmployee(employeeBean));
	}

	/**
	 * 离职员工
	 * @param request
	 * @param employeeBean
	 * @return
	 */
	@RequestMapping(value = "/deleteEmployee.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object deleteEmployee(HttpServletRequest request,@RequestBody EmployeeBean employeeBean) {
		if (employeeBean.getEmployeeId()==null || employeeBean.getEmployeeSeparatedDate()==null) {
			throw new ResultFaultException("0_00001");
		}
		return ResultMap.success(employeeService.deleteEmployee(employeeBean));
	}

	/**
	 * 修改员工
	 * 
	 * @param request
	 * @param employeeBean
	 * @return
	 */
	@RequestMapping(value = "/updateEmployee.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object updateEmployee(HttpServletRequest request, @RequestBody EmployeeBean employeeBean) {
		if (employeeBean.getEmployeeId() == null || StringUtils.isBlank(employeeBean.getChineseName())
				|| StringUtils.isBlank(employeeBean.getEmployeeNo()) || employeeBean.getSex() == null
				|| employeeBean.getEntryDate() == null || employeeBean.getCorporationId() == null
				|| employeeBean.getDepartmentId() == null || employeeBean.getRoleList() == null
				|| employeeBean.getRoleList().size() <= 0 || employeeBean.getFromDate() == null) {
			throw new ResultFaultException("0_00001");
		}
		employeeService.getVerifyEmployee(employeeBean);
		return ResultMap.success(employeeService.updateEmployee(employeeBean));
	}

	/**
	 * 根据EmplyeeId返回Emplyee数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getEmployeeById.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getEmployeeById(HttpServletRequest request) {
		UserEmployeeBean userEmployee = (UserEmployeeBean) request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if (userEmployee == null) {
			throw new ResultFaultException("1_00001");
		}
		return ResultMap.success(employeeService.getEmployeeBeanById(userEmployee.getEmployeeId(), new Date()));
	}

	/**
	 * 重置密码
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/resetEmployeePassWord.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object resetEmployeePassWord(HttpServletRequest request) {
		String employeeIdString = request.getParameter("employeeId");
		if (employeeIdString == null) {
			throw new ResultFaultException("0_00001");
		}
		int employeeId = Integer.parseInt(employeeIdString);
		return ResultMap.success(employeeService.resetEmployeePassWord(employeeId));
	}

	

	/**
	 * 模板下载
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getEmployeeIsSeparatedURL.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody 
	public Object getTemplateUrl(HttpServletRequest request){
		return AppConstant.TEMPLATE_RESOURCE+"/离职信息导入模版.xlsx";
		}

	/**
	 * 编码信息导入接口
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/importEmployeeIsSeparated.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object importEmployeeCode(HttpServletRequest request) throws Exception {
		String attachmentId = request.getParameter("attachmentId");
		if (StringUtils.isBlank(attachmentId)) {
			throw new ResultFaultException("0_00001");
		}
		List<String> successList =employeeService.importEmployeeIsSeparated(Integer.parseInt(attachmentId));
		return ResultMap.success(successList);
	}
	
	/**
	 * 撤回离职人员 zx
	 * @param request
	 * @param employeeBean
	 * @return
	 */
	@RequestMapping(value = "/updateEmployeeByRevocation.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object updateEmployeeByRevocation(HttpServletRequest request,@RequestBody EmployeeBean employeeBean) {
		if (employeeBean.getEmployeeId()==null) {
			throw new ResultFaultException("0_00001");
		}
		return ResultMap.success(employeeService.updateEmployeeByRevocation(employeeBean));
	}
}
